<template>
	<div style="width: 70%;">
		<div class="title"><span>重置密码</span></div>
		<div>
			<wsPanel>
				<template slot="panel_head_title">重置密码</template>
				<div slot="panel_body" class="ws-form-div">
					<div class="ws-form-div-form" v-if="result.code != 0">
						<div class="errorMsg" v-if="result.code != 0">
							<span v-text="result.message"></span>
						</div>
						<Form ref="form" :model="form.model" :rules="form.rules" :label-width="100">
							<FormItem label="设置新密码" prop="password">
								<Input v-model="form.model.password"/>
							</FormItem>
							<FormItem label="确认新密码" prop="comfirepassword">
								<Input v-model="form.model.comfirepassword"/>
							</FormItem>
							<Button  @click="resetpassword">重置密码</Button>
							<Button  @click="goBack">重新登录</Button>
						</Form>
					</div>

				</div>
			</wsPanel>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					model: {
						token:'',
						comfirepassword:'',
						password: ''
					},
					rules: {
						password: [{
								required: true,
								message: "密码不能为空",
								trigger: "blur"
							},
							{
								max: 12,
								message: "密码度不能大于12位",
								trigger: "blur"
							}
						],
						comfirepassword: [{
								required: true,
								message: "确认密码不能为空",
								trigger: "blur"
							},
							{
								max: 12,
								message: "确认密码长度不能大于12位",
								trigger: "blur"
							}
						],
					},
				},
				result: {
					code: -1,
					message: ""
				}
			};
		},
		computed: {},
		mounted() {
			this.getUrlParam();
		},

		

		methods: {
			submit() {
				this.$refs["form"].validate(valid => {
					if(valid) {
						this.resetpassword();
					}else {
                        this.$Message.warning('表格验证失败');
                    }
				});
			},
			/**
			 * 重置密码
			 */
			
			resetpassword() {
				const _this = this;
				let postData_ = this.form.model;
				console.log(postData_);
				if(this.form.model.password===this.form.model.comfirepassword){
				this.axios
					.post("/ws/api/auth/resetpassword", postData_)
					.then(function(response) {
						let res = response.data;
						if(0 === res.meta.code) {
							_this.result.code = 0;
							_this.$Message.success("重置密码成功");
							window.location.href = "./login.html";
						} else {
							let message_ = res.meta.message;
							_this.result.message = message_;
							_this.$Message.warning("重置密码失败:" + message_);
						}
					});
			}else{
				_this.$Message.warning("密码输入不一致，请重新输入");
			}
			},
			/**
             * 获取地址栏参数
            */
		    getUrlParam: function () {
			
				var name,value;
				var str=location.href; //取得整个地址栏
				var num=str.indexOf("?");
				str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]
			
				var arr=str.split("&"); //各个参数放到数组里
				for(var i=0;i < arr.length;i++){
					num=arr[i].indexOf("=");
					if(num>0){
						name=arr[i].substring(0,num);
						if(name==='token'){
						value=arr[i].substr(num+1);
						this[name]=value;
						this.form.model.token = value;
						}
					}
				}
				} ,

			goBack() {
						window.location.href = "./login.html";
					}
				}
	};
</script>

<style>
	html,
	body {
		overflow: auto !important;
	}
	
	.title {
		font-size: 20px;
		text-align: center;
		padding: 15px;
	}
	
	.errorMsg {
		padding-bottom: 20px;
		color: red;
		text-align: center;
		font-size: 18px;
	}
</style>